package org.v2ray.rocket.proxy.activity

import androidx.activity.viewModels
import org.v2ray.rocket.proxy.R
import org.v2ray.rocket.proxy.databinding.ActivityForgetPasswordBinding
import org.v2ray.rocket.proxy.support.base.BaseActivity
import org.v2ray.rocket.proxy.support.user.UserEffect
import org.v2ray.rocket.proxy.support.user.UserIntent
import org.v2ray.rocket.proxy.support.user.UserState
import org.v2ray.rocket.proxy.support.user.UserViewModel
import org.v2ray.rocket.proxy.support.utils.ResourceUtil
import org.v2ray.rocket.proxy.support.utils.showToast
import www.sanju.motiontoast.MotionToastStyle

class ForgetPasswordActivity :
    BaseActivity<ActivityForgetPasswordBinding, UserIntent, UserState, UserEffect, UserViewModel>() {

    override val viewModel: UserViewModel by viewModels<UserViewModel>()


    override fun getViewBinding(): ActivityForgetPasswordBinding {
        return ActivityForgetPasswordBinding.inflate(layoutInflater)
    }

    override fun initView() {
        // 设置点击事件
        binding.btnRetrieve.setOnClickListener {
            // 添加点击动画效果
            binding.btnRetrieve.animate()
                .scaleX(0.95f)
                .scaleY(0.95f)
                .setDuration(100)
                .withEndAction {
                    binding.btnRetrieve.animate()
                        .scaleX(1f)
                        .scaleY(1f)
                        .setDuration(100)
                        .withEndAction {
                            handleRetrievePassword()
                        }
                        .start()
                }
                .start()
        }

        binding.btnCountDown.setOnClickListener {
            // 添加点击动画效果
            binding.btnCountDown.animate()
                .scaleX(0.95f)
                .scaleY(0.95f)
                .setDuration(100)
                .withEndAction {
                    binding.btnCountDown.animate()
                        .scaleX(1f)
                        .scaleY(1f)
                        .setDuration(100)
                        .withEndAction {
                            handleSendVerifyCode()
                        }
                        .start()
                }
                .start()
        }
    }

    override fun initData() {
        // 初始化数据
        setupInputAnimations()
    }

    /**
     * 设置输入框动画效果
     */
    private fun setupInputAnimations() {
        // 手机号输入框焦点动画
        binding.etPhone.setOnFocusChangeListener { _, hasFocus ->
            if (hasFocus) {
                binding.etPhone.animate()
                    .translationX(10f)
                    .setDuration(200)
                    .start()
            } else {
                binding.etPhone.animate()
                    .translationX(0f)
                    .setDuration(200)
                    .start()
            }
        }

        // 密码输入框焦点动画
        binding.etPassword.setOnFocusChangeListener { _, hasFocus ->
            if (hasFocus) {
                binding.etPassword.animate()
                    .translationX(10f)
                    .setDuration(200)
                    .start()
            } else {
                binding.etPassword.animate()
                    .translationX(0f)
                    .setDuration(200)
                    .start()
            }
        }

        // 验证码输入框焦点动画
        binding.etSmsCode.setOnFocusChangeListener { _, hasFocus ->
            if (hasFocus) {
                binding.etSmsCode.animate()
                    .translationX(10f)
                    .setDuration(200)
                    .start()
            } else {
                binding.etSmsCode.animate()
                    .translationX(0f)
                    .setDuration(200)
                    .start()
            }
        }
    }

    override fun renderState(state: UserState) {
        // 更新UI状态
        binding.btnRetrieve.isEnabled = !state.isLoading
        binding.btnCountDown.isEnabled = !state.isLoading

        // 找回密码按钮加载动画
        if (state.isLoading) {
            // 开始加载动画
            binding.btnRetrieve.animate()
                .alpha(0.8f)
                .scaleX(0.95f)
                .scaleY(0.95f)
                .setDuration(300)
                .setInterpolator(android.view.animation.AccelerateDecelerateInterpolator())
                .withEndAction {
                    // 添加脉冲动画效果
                    binding.btnRetrieve.animate()
                        .alpha(0.6f)
                        .setDuration(800)
                        .setInterpolator(android.view.animation.LinearInterpolator())
                        .withEndAction {
                            binding.btnRetrieve.animate()
                                .alpha(0.8f)
                                .setDuration(800)
                                .setInterpolator(android.view.animation.LinearInterpolator())
                                .start()
                        }
                        .start()
                }
                .start()
        } else {
            // 结束加载动画
            binding.btnRetrieve.animate()
                .alpha(1f)
                .scaleX(1f)
                .scaleY(1f)
                .setDuration(300)
                .setInterpolator(android.view.animation.AccelerateDecelerateInterpolator())
                .start()
        }
    }

    override fun renderEffect(effect: UserEffect) {
        when (effect) {
            is UserEffect.ResetPasswordSuccess -> {
                // 找回密码成功，返回登录页面
                finish()
            }
            else -> {}
        }
    }

    /**
     * 处理找回密码
     */
    private fun handleRetrievePassword() {
        val phoneNumber = binding.etPhone.text.toString().trim()
        val password = binding.etPassword.text.toString().trim()
        val smsCode = binding.etSmsCode.text.toString().trim()

        if (phoneNumber.isEmpty()) {
            showToast(
                style = MotionToastStyle.WARNING,
                message = ResourceUtil.resourceString(R.string.please_enter_phone_number)
            )
            return
        }

        if (password.isEmpty()) {
            showToast(
                style = MotionToastStyle.WARNING,
                message = ResourceUtil.resourceString(R.string.please_enter_password)
            )
            return
        }

        if (smsCode.isEmpty()) {
            showToast(
                style = MotionToastStyle.WARNING,
                message = ResourceUtil.resourceString(R.string.please_enter_verification_code)
            )
            return
        }

        // 调用找回密码接口
        setIntent(UserIntent.ResetPassword(phoneNumber, smsCode, password))
    }

    /**
     * 处理发送验证码
     */
    private fun handleSendVerifyCode() {
        val phoneNumber = binding.etPhone.text.toString().trim()
        if (phoneNumber.isEmpty()) {
            showToast(
                style = MotionToastStyle.WARNING,
                message = ResourceUtil.resourceString(R.string.please_enter_phone_number)
            )
            return
        }
        setIntent(UserIntent.SendVerifyCode(phoneNumber))
    }
}